package com.uenpay.im.ui.old.activity.search;

import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-2-2 下午4:51:56
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class SearchPeopleInvokeItem extends BaseHttpInvokeItem {

	public class SearchPeopleInvokeItemResult extends HttpInvokeResult {
		public ArrayList<QYXUserEntity> arrayList = new ArrayList<QYXUserEntity>();
	}

	public SearchPeopleInvokeItemResult getOutPut() {
		return (SearchPeopleInvokeItemResult) GetResultObject();
	}

	public SearchPeopleInvokeItem(int page, int sex, String area) {
		String searchSex = "" + sex;
		if (sex == 2) {
			searchSex = "";
		}
		// http://api.qiyunxin.com/Personell/Personell/search?sex=&area=interests=&pageIndex=&pageSize=
		String url = APIConfiguration.getBaseUrl()
				+ "Personell/Personell/search?sex=" + searchSex + "&area="
				+ area + "&interests=&pageIndex=" + page + "&pageSize=12&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		SearchPeopleInvokeItemResult result = new SearchPeopleInvokeItemResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		if (result.status != 0) {
			return result;
		}
		JSONArray dataJson = json.optJSONArray("data");
		if (dataJson == null) {
			return result;
		}
		for (int i = 0, size = dataJson.length(); i < size; i++) {
			result.arrayList.add(desQYXUserEntity(dataJson.optJSONObject(i)));
		}
		return result;
	}

	private QYXUserEntity desQYXUserEntity(JSONObject json) {
		QYXUserEntity mQYXUserEntity = new QYXUserEntity();
		mQYXUserEntity.custid = json.optString("custid");
		mQYXUserEntity.nickname = json.optString("nickname");
		mQYXUserEntity.sex = json.optInt("sex");
		mQYXUserEntity.area = json.optString("area");
		return mQYXUserEntity;
	}
}
